package com.auto.learning.receiver;

import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

import com.auto.learning.MainActivity;
import com.auto.learning.activity.BookListActivity;
import com.auto.learning.activity.MyMessageActivity;
import com.auto.learning.activity.PlayActivity;
import com.auto.learning.activity.SplashActivity;
import com.auto.learning.activity.WebActivity;
import com.auto.learning.bean.Banner_H5;
import com.auto.learning.bean.BookListModel;
import com.auto.learning.event.EventBus;
import com.auto.learning.event.MainShowTipEvent;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 极光推送接收消息
 * @author jianjun
 *
 */
public class JPushReceiver extends BroadcastReceiver {

	private final static String TAG = JPushReceiver.class.getSimpleName();
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive - " + intent.getAction());
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
        	
        }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {   //自定义消息不会展示在通知栏，完全要开发者写代码去处理

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {  // 收到了通知
        	String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        	if (!TextUtils.isEmpty(extras)) {
        		try {
					JSONObject jsonData = new JSONObject(extras);
					int type = jsonData.getInt("type");  //type: 1 一般通知，2 推荐书，3推荐书单
					if (type == 1) {
						EventBus.getEventBus("JPush_show_tip_red").post(new MainShowTipEvent(true));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {  //用户点击通知
			String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);  //消息的附件信息
			int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); //服务器推送下来的内容类型。
			Log.d(TAG, extras);
			if (!TextUtils.isEmpty(extras)) {
				Intent mIntent = null;
				try {
					JSONObject jsonData = new JSONObject(extras);
					int type = jsonData.getInt("type");  //type: 1 一般通知，2 推荐书，3推荐书单
					if (TemporaryVariable.activities.isEmpty()) {
						mIntent = new Intent(context, SplashActivity.class);	
						mIntent.putExtra("JPush_Data", extras);
					} else {
						String web_h5 = jsonData.optString("h5", "");
						if (!TextUtils.isEmpty(web_h5)) {
							Banner_H5 banner_H5 = new Gson().fromJson(web_h5, new TypeToken<Banner_H5>() {}.getType());
							if (banner_H5 != null) {
								mIntent = new Intent(context, WebActivity.class);
								mIntent.putExtra("url", banner_H5.getH5().replace("{userId}", String.valueOf(SharedPreferencesUtils.getParam(context, HttpRequestHelper.USERID, 0))));
								mIntent.putExtra("showtitle", true);
								mIntent.putExtra("title",banner_H5.getName()+"");
								mIntent.putExtra("h5_model", banner_H5);
							}
						}else{
							switch (type) {
							case 1:
								mIntent = new Intent(context, MyMessageActivity.class);
								mIntent.putExtra("msg_type", 1);
								break;
							case 2:
								int bookId = jsonData.getInt("id");
								String faceImg = jsonData.getString("faceImg");
								int canRead = jsonData.optInt("canRead", 2);
							
								TemporaryVariable.read_list.clear();
								TemporaryVariable.read_list.add(bookId);
								if (TemporaryVariable.playActivity != null && TemporaryVariable.playActivity instanceof PlayActivity) {
									TemporaryVariable.playActivity.finish();
								}
								mIntent = new Intent(context, PlayActivity.class);
								mIntent.putExtra("canRead", canRead);
								mIntent.putExtra("bookId", bookId);
								mIntent.putExtra("faceImg", faceImg);
							
								mIntent.putExtra("type", 1);
								break;
							case 3:
								int listId = jsonData.getInt("id");
								mIntent = new Intent(context, BookListActivity.class);
						    	BookListModel bookListModel = new BookListModel();
						    	bookListModel.setListId(listId);
						    	mIntent.putExtra("data", bookListModel);
								break;
							}
						}
					}
					mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(mIntent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
        }
	}
}
